package facilius.controller.action.aula;

import facilius.model.ServiceLocator;
import facilius.model.pojo.Aula;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import org.mentawai.action.BaseLoginAction;
import org.mentawai.core.BaseAction;

/**
 *
 * @author Graciano
 */
public class AulaShowFormAction extends BaseAction{

    @Override
    public String execute() throws Exception {
        String consequence = ERROR;
        this.preload();
        try {
            Long id = input.getLong("id");
            String formAction = "AulaCreate.mtw";
            if (id != null && id > 0){
                Aula aula = ServiceLocator.getAulaService().readById(id);
                output.setValue("id", aula.getId());
                output.setValue("data", converteData(aula.getData()));
                output.setValue("descricao", aula.getDescricao());
                output.setValue("titulo", aula.getTitulo());
                output.setValue("turmaId", aula.getTurma().getId());
                formAction = "AulaUpdate.mtw";
            }
            output.setValue("formAction", formAction);
            consequence = SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consequence;
    }

    private String converteData(java.util.Date dtData) {
        SimpleDateFormat out = new SimpleDateFormat("dd/MM/yyyy");
        return out.format(dtData);
    }

    public void preload() throws Exception {
        Map<String, Object> criteria = new HashMap<String, Object>();
        criteria.put("professor",session.getAttribute("userId"));
        Map<String, String> options = ServiceLocator.getTurmaService().getOptions(criteria);
        output.setValue("turmaOptions", options);
    }

}
